/* ---------------------------------------------------------------------
 * palette.c
 *
 * Functions dealing with the palette
 *
 * --------------------------------------------------------------------*/

#define _PALETTE_

#include <io.h>
#include <i86.h>
#include <fcntl.h>
#include <dos.h>
#include <stdlib.h>
#include <stdio.h>
#include <mem.h>

#include "rad.h"
#include "smack.h"
#include "player.h"
#include "mouse.h"
#include "sprite.h"
#include "event.h"
#include "sound.h"
#include "palette.h"
#include "svga.h"
#include "debug.h"

void shiftPalette();
void printPalette(u8 *p, char *str);

// ----------------------------------------------------------------
//    Global Variables
// ----------------------------------------------------------------

u8 *g_top64=0;
u8 *g_palette=0;		// A pointer to the current palette
u8 *g_panelpalette= NULL;		// A pointer to the current palette
// ----------------------------------------------------------------------
//   setPal
//
// For changing the color palette.  Updates the global g_palette, and
// dumps the whole thing into the VGA card.  Note that palette setting is
// a tricky manuver and can easily cause nasty flashes.
// ----------------------------------------------------------------------
	
void setPal(u8 *pal,long start,long numcol)
{
 	u8 *p=g_palette;
	long i;

	p=p+(3*start);
	for(i=0;i<numcol;i++)
	{
		*p++ = *pal++;
		*p++ = *pal++;
		*p++ = *pal++;
	}

	PaletteSet(g_palette);	// A smacker library function
}

void ClearPalette()
{
	u8 *p;
	long i;

	if(!g_palette)
		g_palette=malloc(PALSIZE);

   memset(g_palette,0,PALSIZE);
}

void startPaletteFlash(long r,long g,long b,long frames)
{
	g_flash.r = (r/frames);
	g_flash.g = (g/frames);
	g_flash.b = (b/frames);

	memcpy(g_flash.palette,g_palette,256*3);

//	printPalette(g_flash.palette , "start Palette");
	g_flash.timer = frames;

	//Add one so that we can go back to 
	//the original palette at 1
	g_flash.timer++;

}

void updatePaletteFlash()
{

	if(g_flash.timer)
	{
		if (g_player.hitpoints <= 0)
		{
			PaletteSet(g_palette);
			g_flash.timer=0;
			return;
		}
		shiftPalette();
		PaletteSet(g_flash.palette);
//		printPalette(g_flash.palette,"Flash Pal");

		g_flash.timer--;
		if (!g_flash.timer)
		{
			
			PaletteSet(g_palette);
		}
	}
}


void printPalette(u8 *p, char *str)
{

//#ifdef DEBUGGING_ON
//	int c;
//
//	fprintf(dFile,"%s\n______________________\n",str);
//
//	for (c=0; c<256; c++)
//	{
//			fprintf(dFile,"Row (%3d)  R: %3d G:%3d B: %3d\n",c,
//					*p++,*p++,*p++);
//
//	}
//	fprintf(dFile,"\n");
//#endif 
}


void shiftPalette()
{

	long i,j;

	u8 *fp = g_flash.palette;
	fp+=3;// NOTE: put this in to go by the 1st member of the palette


	for(i=1;i<193;i++)
	{
		j=*fp;

		switch (g_flash.timer)
		{
			case 1:
			case 2:
			*fp++ = g_flash.r;		
				break;
			case 3:
				*fp++ = (g_flash.r/2);		
				break;
		}

		j=*fp;
		j+=g_flash.g;
		if(j>255)
		{
			*fp++ = 255;
		}
		else
		{
			*fp++ = j;
		}

		j=*fp;
		j+=g_flash.b;
		if(j>255)
		{
			*fp++ = 255;
		}
		else
		{
			*fp++ = j;
		}			 
	}
}

void showPalette(u8 *sb)
{
	int c;
	int d;

	//Bring me down by 180 rows (Y)
	sb += (352 * 180);

	for (d=0; d<10; d++)
	{
		for (c=0; c<256; c++)
		{
			*sb++ = c; 
		}
		sb += (352 - 256);
	}
}


void fadeWholePalToBlack(int frames)
{
 	u8 *tempPal2;
	int i;
	int f;
	int c;


	tempPal2 =  malloc(PALSIZE);
 	if (!tempPal2)
	{
		fatalerror("Couldn't load palette");
	}
	for(f=frames; f>0;f--)	
	{
		int offset = 0;
		for(i=0;i<256;i++)	
		{
			for(c=0;c<3;c++)	
			{
				*(tempPal2 + offset) = *(g_palette + offset) * f / frames;
				offset++;
			}
		}
		PaletteSet(tempPal2);	// A smacker library function
	}
	free(tempPal2);
}

void fadeWholePalToNewPal(int frames,u8 *pal)
{
 	u8 *tempPal;
	int i;
	int f;
	int c;


	tempPal =  malloc(PALSIZE);
 	if (!tempPal)
	{
		fatalerror("Couldn't load palette");
	}
	
	for(f=frames; f>0;f--)	
	{
		int offset = 0;
		for(i=0;i<256;i++)	
		{
			for(c=0;c<3;c++)	
			{
				*(tempPal + offset) = *(g_palette + offset) * f / frames;
				offset++;
			}
		}
		PaletteSet(tempPal);	// A smacker library function
	}
}

void setPalToBlack()
{
	// Set first 192 regs to black
	memset(g_palette, 0, 192*3);
	PaletteSet(g_palette);	// A smacker library function
}

void pushTop64Entries()
{
	if (!g_top64)
	{
		g_top64 = malloc(64*3);
	}
	memcpy(g_top64, g_palette+(192*3), 64*3);
	if (!g_top64)
	{
		fatalerror("Couldn't malloc palette");
	}
}

void popTop64Entries()
{
	if (!g_top64)
	{
		fatalerror("Bad Palette Pointer");
	}
	memcpy(g_palette+(192*3), g_top64, 64*3);
}
